package org.ajc.org.service.impl;


import org.ajc.base.service.impl.BaseServiceImpl;
import org.ajc.org.domain.Department;
import org.ajc.org.mapper.DepartmentMapper;
import org.ajc.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void insert(Department department) {
        departmentMapper.insert(department);
        //添加path
        //是否有上级部门
        if(Objects.isNull(department.getParent().getId())){
            department.setPath("/"+department.getId());
        }else{
            //查询上级部门
            Department department1 = departmentMapper.selectById(department.getParent().getId());
            department.setPath(department1.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }


    @Override
    @Transactional
    public void update(Department department) {
        if(Objects.isNull(department.getParent().getId())){
            department.setPath("/"+department.getId());
        }else{
            //查询上级部门
            Department department1 = departmentMapper.selectById(department.getParent().getId());
            department.setPath(department1.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }


    @Override
    public List<Department> deptTree() {
        return departmentMapper.deptTree();
    }

}
